<template>
  <div style="">
    <uploader
      ref="uploader"
      :options="options"
      :autoStart="true"
      :file-status-text="fileStatus"
      class="uploader-example"
      @file-added="onFileAdded"
      @files-added="onFilesAdded"
      @file-error="onFileError"
      @file-complete="onFileComplete"
      @file-success="onFileSuccess"
    >
      <uploader-unsupport /> 
      <div style="padding:10px;">
        <uploader-btn ref="uploadFolderBtn" class="uploader_btn" :directory="true">
          <uploader-drop class="drop">
            <div class="drop1">
              <div class="drop2">
                拖动上传多个文件夹,点击上传单个文件夹
              </div>
            </div>
          </uploader-drop>
        </uploader-btn>
      </div>
      <div class="drog_list">
        <uploader-list v-show="panelShow">
          <div class="file-panel" slot-scope="props" :class="{ collapse: collapse }">
            <div class="file-title">
              <div class="title">文件列表</div>
              <div class="operate">
                <el-button @click="collapse = !collapse" type="text" :title="collapse ? '展开' : '折叠'">
                  <i class="iconfont" :class="collapse ? 'el-icon-full-screen' : 'el-icon-minus'"></i>
                </el-button>
                <el-button @click="CloseFilesUploadList" type="text" title="关闭">
                  <i class="el-icon-close"></i>
                </el-button>
              </div>
            </div>
            <ul class="file-list">
              <li class="file-item" v-for="file in props.fileList" :key="file.id">
                <uploader-file
                  :class="['file_' + file.id]"
                  ref="files"
                  :file="file"
                  :list="true"
                >
                </uploader-file>
              </li>
              <div class="no-file" v-if="!props.fileList.length">
                <i class="iconfont icon-empty-file"></i> 暂无待上传文件
              </div>
            </ul>
          </div>
        </uploader-list>
      </div>
    </uploader>
  </div>
</template>
<script>
import { uploadImg ,postManyword} from "../../api/api";
export default {
  data () {
    return {
      fileList:[],
      fileName:'',
      options: {
        target: "http://xxx.xxx.com/wechat_upload/",//上传地址，如果有文件上传地址不同时 可以是个函数来改变
        headers: {
          Authorization: 'JWT ' + window.sessionStorage.getItem("token"),
        },
        testChunks:false,
        chunkSize:50*1024*1024   // 50Mb
      },
      fileStatus: {//显示的状态
        success: "上传成功",
        error: "上传失败",
        uploading: "上传中",
        paused: "已暂停",
        waiting: "等待上传",
      },
      panelShow: false,
      collapse: false,
      imageArr:[]
    }
  },
  
  computed: {
    // 获取上传文件实例
    uploaderRef() {
      return this.$refs.uploader.uploader;
    },
  },
  
  methods: {
    // 全部文件处理 
    onFilesAdded (files, filelist) {
      this.panelShow = true
    },
    // 文件上传获取单个文件
    onFileAdded (file, fileList) {
    },
    
    // 根文件上传成功
    onFileComplete (rootFile) {
      this.fileName=rootFile.files[0].file.relativePath.split('/')[0]
      console.log(this.fileName);
      console.log(this.imageArr);
      postManyword({
        content:this.fileName,
        images:this.imageArr
      }).then((res)=>{
        console.log(res);
        this.$message({
          message: "上传成功！",
          type: "success",
        });
        this.imageArr=[]
        this.fileName=''
      }).catch((err)=>{
        console.log(err);
        this.$message({
          message: "上传失败！",
          type: "error",
        });
        this.imageArr=[]
        this.fileName=''
      })
    },
    onFileSuccess(rootFile, file, message, chunk){
      let message1=JSON.parse(message)
      this.imageArr.push(message1.file)
    },
    // 文件上传失败
    onFileError (file) {
      this.$message({
        message: "上传失败，请重试！",
        type: "error",
      });
    },
    // 点击关闭按钮
    CloseFilesUploadList () {
      this.uploaderRef.cancel();
      this.panelShow = false
    },
  }
};
</script>
<style lang="scss" scoped>
.drop{
  height: 600px;
  display: flex;
  margin: 0 auto;
}
.drop1{
  position: relative;
  width: 100%;
}
.drop2{
  position: absolute;
  top:50%;
  left: 50%;
  transform: translate(-50%);
}
.uploader_btn {
  padding: 20px;
  width: 100%;
  background-color: rgb(77, 219, 160);
}
.drog_list {
  position: fixed;
  z-index: 20;
  right: 15px;
  bottom: 15px;
  width: 600px;
  box-sizing: border-box;
  .file-panel {
    background-color: #fff;
    border: 1px solid #e2e2e2;
    border-radius: 7px 7px 0 0;
    box-shadow: 0 0 10px rgba(0, 0, 0, 0.2);

    .file-title {
      display: flex;
      height: 40px;
      line-height: 40px;
      padding: 0 15px;
      border-bottom: 1px solid #ddd;

      .operate {
        flex: 1;
        text-align: right;

        i {
          font-size: 18px;
        }
      }
    }

    .file-list {
      position: relative;
      height: 240px;
      overflow-x: hidden;
      overflow-y: auto;
      background-color: #fff;
      transition: all 0.3s;
      list-style: none;
      padding: 0 2%;
      font-size: 12px;
      .file-item {
        background-color: #fff;
      }

      ::v-deep .uploader-file-size {
        text-indent: 0 !important;
      }
      ::v-deep .uploader-file-status {
        width: 22%;
      }
      ::v-deep .uploader-file-name {
        display: flex;
        align-items: center;
        .uploader-file-icon {
          margin-top: 0;
          margin-right: 0;
        }
      }
      ::v-deep .uploader-file-meta {
        display: none;
      }
      ::v-deep .uploader-file-actions {
        width: 12%;
        float: right;
      }
    }

    &.collapse {
      .file-title {
        background-color: #e7ecf2;
      }
      .file-list {
        height: 0;
      }
    }
  }
}
.file-panel {
    background-color: #fff;
    border: 1px solid #e2e2e2;
    border-radius: 7px 7px 0 0;
    box-shadow: 0 0 10px rgba(0, 0, 0, 0.2);

    .file-title {
      display: flex;
      height: 40px;
      line-height: 40px;
      padding: 0 15px;
      border-bottom: 1px solid #ddd;

      .operate {
        flex: 1;
        text-align: right;

        i {
          font-size: 18px;
        }
      }
    }

    .file-list {
      position: relative;
      height: 240px;
      overflow-x: hidden;
      overflow-y: auto;
      background-color: #fff;
      transition: all 0.3s;

      .file-item {
        background-color: #fff;
      }
    }

    &.collapse {
      .file-title {
        background-color: #e7ecf2;
      }
      .file-list {
        height: 0;
      }
    }
  }
</style>
